home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Self-Test

Program Bad6 violates several of the guidelines presented in this chapter. This program loads data type information from a data file and displays a summary as shown in Figure 6.1.

The following code shows how program Bad6 works. Appendix A, “Self-Test Solutions,” contains an improved version of this code. This is not the only possible solution. You can download updated bad and good versions of this program from the Web at
www.vb-helper.com/err.htm.

Note: In the following code, the line

If Len(token) > 0 Then output_txt = output_txt & Format(token,
FORMAT_SPECIFIER)

should extend as far as it can and then be truncated at the right edge of the page. It should not be wrapped. Do not extend it beyond the margin or anything fancy like that. Just chop it off, in mid-word if that’s where the page ends (that would make the best example). It is demonstrating code that does not wrap.


Figure 6.1  Program Bad6 loads data type information from a data file and displays a summary.

Option Explicit

‘ Make txtOutput as large as possible.
Private Sub Form_Resize()
    txtOutput.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

‘ Read the data from the data file.
Private Sub Form_Load()
Dim fnum As Integer

    ‘ Open the file.
    fnum = FreeFile
    Open App.Path & “\types.dat” For Input As fnum

    ‘ Read the data.
    LoadData fnum
End Sub

‘ Read the data file and close it.
Sub LoadData(fnum As Integer)
Const FORMAT_SPECIFIER = “!@@@@@@@@@@@@@@@@@@@@”

Dim output_txt As String
Dim all_data As String
Dim next_line As String
Dim token As String

    ‘ Read all the data.
    all_data = Input(LOF(fnum), #fnum)

    ‘ Start with column headers.
    output_txt = _
        Format(“Data Type”, FORMAT_SPECIFIER) & _
        Format(“Size”, FORMAT_SPECIFIER) & _
        Format(“Approx Min”, FORMAT_SPECIFIER) & _
        Format(“Approx Max”, FORMAT_SPECIFIER)
    output_txt = output_txt & vbCrLf & _
        Format(“---------”, FORMAT_SPECIFIER) & _
        Format(“----”, FORMAT_SPECIFIER) & _
        Format(“----------”, FORMAT_SPECIFIER) & _
        Format(“----------”, FORMAT_SPECIFIER) & _
        vbCrLf

    ‘ Read the lines from the file.
    Do While Len(all_data) > 0
        ‘ Get the next line.
        next_line = GetToken(all_data, vbCrLf)
    
        ‘ Break the line into pieces.
        Do While Len(next_line) > 0
            token = GetToken(next_line, “;”)
            If Len(token) > 0 Then output_txt = output_txt & Format(token, FORMAT_SPECIFIER)
        Loop
        output_txt = output_txt & vbCrLf
    Loop

    ‘ Display the result.
    txtOutput = output_txt
End Sub

‘ Get the next delimited token from the string txt.
Function GetToken(txt As String, delimiter As String) As String
Dim p As Integer

    ‘ Find the delimiter.
    p = InStr(txt, delimiter)

    ‘ Get the token.
    If p = 0 Then p = Len(txt) + 1
    GetToken = Left(txt, p - 1)

    ‘ See what’s left of the string txt.
    p = Len(txt) - p + 1 - Len(delimiter)
    If p <= 0 Then
        txt = “”
    Else
        txt = Right(txt, p)
    End If
End Function

Summary

Maintainable code must be easy to understand. When someone later reads the code, any extra time spent struggling to make sense out of trivial details like variable names and scope is time that could be better spent studying the program’s structure. Make your code easy to understand by using the techniques summarized in the following Bug Stoppers.

BUG STOPPERS: Being Obvious
Don’t use clever tricks.
Document any clever tricks you do use.
Don’t write routines with side effects.
Mix data types cautiously.
Use & and + when appropriate.
Mix Booleans and integers carefully.
Parenthesize complex expressions.
Use the string and variant version of Left$, Mid$, etc.
Open and close files in the same routine.
Put separate commands on separate lines.
Explicitly specify lower bounds for arrays.
Don’t use static routines.
Always use Private and Public to specify module-level symbols.
Always use ByVal and ByRef to make calling conventions obvious.
Use line continuation so all the code is visible.
Refer to properties explicitly.
Eliminate or control random behavior.
Perform short actions first in If ... Else and Select statements.
Use wizards cautiously with an all-or-nothing approach.
Use meaningful names, not terse abbreviations.
Standardize any abbreviations you do use.
Use similar names for similar purposes. Use dissimilar names for dissimilar purposes.
Don’t reuse variables.
Use the loop variable’s name in Next statements.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.